package org.platform.lwc.system.feign;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import org.platform.lwc.log.annotation.ApiLog;
import org.platform.lwc.system.entity.SysRole;
import org.platform.lwc.system.entity.SysUser;
import org.platform.lwc.system.service.ISysRoleService;
import org.platform.lwc.system.service.ISysUserService;
import org.platform.lwc.tool.api.R;
import org.platform.lwc.tool.auth.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 系统服务Feign实现类
 *
 * @author lwc
 */
@RestController
public class SysUserClient implements ISysUserClient {

    @Autowired
    private  ISysUserService userService;
    @Autowired
    private  ISysRoleService roleService;



    /**
     * 通过查用户的全部信息
     *
     * @param account
     * @return
     */
    @Override
    @ApiLog
    public R<LoginUser> userInfo(String account) {
        SysUser sysUser = userService.getOne(
                new QueryWrapper<>(new SysUser()).lambda()
                        .eq(SysUser::getAccount, account)
                        .eq(SysUser::getIsDeleted, false));
        if (ObjectUtil.isNotEmpty(sysUser)) {
            return R.data(disposeUserInfo(sysUser));
        }
        return R.data(null);
    }


    private LoginUser disposeUserInfo(SysUser sysUser) {

        //角色数据
        List<SysRole> roles = roleService.getSysRoles(sysUser.getId());
        List<String> roleKeys = Lists.newArrayList();
        Set<String> authPermissions = Sets.newConcurrentHashSet();
        if (CollUtil.isNotEmpty(roles)) {
            //角色权限
            roleKeys = roles.stream().map(SysRole::getRoleKey).collect(Collectors.toList());

        }
        return LoginUser.builder()
                .id(sysUser.getId())
                .username(sysUser.getName())
                .password(sysUser.getPassword())
                .status(sysUser.getStatus())
                .roles(roleKeys)
        .build();

    }


}
